﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using PhoenixCrabV6.Images;
using PhoenixCrabV6.XML;

namespace PhoenixCrabV6.Screens
{
    public class ScreenManager
    {
        
        private static ScreenManager instance;
        public Vector2 Dimensions;

        [XmlIgnore]
        public ContentManager Content { private set; get; }
        private XmlManager<GameScreen> xmlGameScreenManager;

        private GameScreen currentScreen, newScreen;
        [XmlIgnore]
        public GraphicsDevice GraphicsDevice;
        [XmlIgnore]
        public SpriteBatch SpriteBatch;

        //Residual of former screen/fadeimage
        public Image Image;
        [XmlIgnore]
        public bool IsTransitioning { get; private set; }

        
        public static ScreenManager Instance
        {
            get
            {
                if (instance == null)
                {
                    XmlManager<ScreenManager> xml = new XmlManager<ScreenManager>();
                    instance = xml.Load("XML/Screens/ScreenManager.xml");
                }
                    

                return instance;
            }
        }
        
        public void ChangeScreens(string screenName)
        {
            newScreen = (GameScreen) Activator.CreateInstance(Type.GetType("PhoenixCrabV6.Screens." + screenName));
            Image.IsActive = true;
            Image.FadeEffect.Increase = true;
            Image.Alpha = 0.0f;
            IsTransitioning = true;
        }

        void Transition(GameTime gameTime)
        {
            if (IsTransitioning == true)
            {
                Image.Update(gameTime);
                if (Image.Alpha == 1.0f)
                {
                    currentScreen.UnloadContent();
                    currentScreen = newScreen;
                    xmlGameScreenManager.Type = currentScreen.Type;

                    if (File.Exists(currentScreen.XmlPath))
                    {
                        currentScreen = xmlGameScreenManager.Load(currentScreen.XmlPath);
                    }
                    currentScreen.LoadContent();

                }
                else if (Image.Alpha == 0.0f)
                {
                    Image.IsActive = false;
                    IsTransitioning = false;
                }
            }
        }

        public ScreenManager()
        {
            //get the dimensions from the xml file
            //Dimensions = new Vector2(640,480);
            currentScreen = new SplashScreen();
            xmlGameScreenManager = new XmlManager<GameScreen>();
            xmlGameScreenManager.Type = currentScreen.Type;
            currentScreen = xmlGameScreenManager.Load(currentScreen.XmlPath/*"XML/Screens/SplashScreen.xml"*/);

           
        }

        public void LoadContent(ContentManager Content)
        {
            this.Content = new ContentManager(Content.ServiceProvider, "Content");
            currentScreen.LoadContent();
            Image.LoadContent();
        }

        public void UnloadContent()
        {
            currentScreen.UnloadContent();
            Image.UnloadContent();
        }

        public void Update(GameTime gameTime)
        {
            currentScreen.Update(gameTime);
            Transition(gameTime);

            /*if (Keyboard.GetState().IsKeyDown(Keys.Enter) && !ScreenManager.instance.IsTransitioning)
                ScreenManager.instance.ChangeScreens("SplashScreen"); */
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            currentScreen.Draw(spriteBatch);
            if(IsTransitioning)
                //if it is transitioning, draw the residual of the former screen
                Image.Draw(spriteBatch);
        }



        public void SetResolution(GraphicsDeviceManager graphics)
        {
            graphics.PreferredBackBufferWidth = (int)ScreenManager.Instance.Dimensions.X;
            graphics.PreferredBackBufferHeight = (int)ScreenManager.Instance.Dimensions.Y;
            graphics.ApplyChanges();
        }
    }
}
